

//930.和相同的二元子数组
class Solution {
public:
    int numSubarraysWithSum(vector<int>& nums, int goal) {
        int n = nums.size();
        unordered_map<int,int> m;   //存储每一个前缀和以及个数
        m.insert({0,1});            //将0提前加入,使得[0,i]完整的子数组也会被统计

        int sum = 0, ret = 0;
        for(int i = 0; i < n ;i++)
        {
            sum += nums[i];
            if(m.count(sum - goal)) ret += m[sum-goal];
            m[sum]++;
        }
        return ret;
    }
};